package com.example.demo01.demo;

import static android.view.View.GONE;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.demo01.R;
import com.example.demo01.fragments.TitleFragment;
import com.example.demo01.utils.ToastUtil;

public class AlertDialogDemo extends AppCompatActivity {

    Button btn_alert1, btn_alert2, btn_alert3, btn_popup1;

    ToastUtil toastUtil = new ToastUtil(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alert_dialog);
        EdgeToEdge.enable(this);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        //. 添加标题
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fl_title, TitleFragment.newInstance(getResources().getString(R.string.AlertDialogDemo)))
                .commit();

        initView();
    }

    private void initView() {
        /// 普通对话框
        btn_alert1 = findViewById(R.id.btn_alert1);
        /// 列表对话框
        btn_alert2 = findViewById(R.id.btn_alert2);
        /// 自定义对话框
        btn_alert3 = findViewById(R.id.btn_alert3);

        //. 给普通对话框添加点击事件
        btn_alert1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAlertDialog();
            }
        });

        //. 给列表对话框添加点击事件
        btn_alert2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showItemsDialog();
            }
        });

        //. 给自定义对话框添加点击事件
        btn_alert3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCustomDialog();
            }
        });

    }

    /// 普通对话框
    private void showAlertDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        builder.setIcon(R.drawable.tx_1)
                .setTitle("系统提示")
                .setMessage("你手机快冇电了，请及时充电")
                .setNeutralButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        toastUtil.showToast("点击了确定");
                    }
                }).create().show();
    }

    /// 列表对话框
    private void showItemsDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        String[] arr = {"选项1", "选项2", "选项3", "选项4", "选项5"};
        builder.setItems(arr, new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                toastUtil.showToast("点击了: " + arr[which]);
            }
        }).create().show();
    }

    /// 自定义对话框
    private void showCustomDialog() {
        View view = LayoutInflater
                .from(this)
                .inflate(R.layout.activity_alert_dialog_custom, null);

        ((TextView) view.findViewById(R.id.title)).setText("系统消息");
        ((TextView) view.findViewById(R.id.message)).setText("确定关闭吗？");

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(view); //. 设置自定义对话框
        AlertDialog dialog = builder.create(); //. 创建对话框
        dialog.show(); //. 显示对话框

        view.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toastUtil.showToast("点击了取消");
                dialog.dismiss(); //. 关闭对话框
            }
        });
        view.findViewById(R.id.confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toastUtil.showToast("点击了确定");
                dialog.dismiss(); //. 关闭对话框
            }
        });

    }


    //! 清除非本类视图
    void clearView() {
        btn_popup1 = findViewById(R.id.btn_popup1);
        btn_popup1.setVisibility(GONE);
    }
}